package util;


import hibernate.mappings.MsNode;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SocketRenticc {
	private static String url="http://www.mtrtys.com/";
	private static int number = 2791;
	private static Map<Integer, MsNode> m = new HashMap<Integer, MsNode>();
	public static void download(int num,String href,String title) { // if(true)return 1;
		String path="F:/1/Renticc/"+num+title+"/";
		File dir = new File(path);
		if (!dir.exists())
			dir.mkdirs();
		int i = 1;// http://img.rentiyishu.org/image/4623/11.jpg
		for (; i < 99; i++) {
//			String tempurl = href;
//			if (i < 10)
//				tempurl = tempurl + "0";
			String tempurl = href + i + ".jpg";
			URL url;
			try {
				// BufferedReader reader = new BufferedReader(new
				// InputStreamReader(
				// conn.getInputStream()));
				File file = new File(path + i + ".jpg");

				url = new URL(tempurl);
				// URLConnection conn = url.openConnection();
				InputStream in = url.openStream();
				BufferedInputStream bufIn = new BufferedInputStream(in);
				BufferedOutputStream bufOut = new BufferedOutputStream(
						new FileOutputStream(file));
				byte[] b = new byte[1];
				while (bufIn.read(b) != -1) {
					bufOut.write(b);
				}
				bufOut.flush();
				bufOut.close();
				bufIn.close();
				// System.out.println(file.length());
				if (file.length() == 69634l) {
					file.delete();
					break;
				}

			} catch (Exception e) {
				break;
			}
		}
	}

	public static void main(String[] args) throws ParseException {
//		SocketRenticc client = new SocketRenticc();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//		long l=sdf.parse(day).getTime();
//		long time=System.currentTimeMillis();
//		String temp="";
//		while (l < time) {
//			temp=sdf.format(new Date(l));
//			client.download(temp);
//			System.out.println("finish:"+temp);
//			l=l+86400000l;
//		}
String tempContent=getDocumentAt(url+"rentiyishuwaipai/");
		List<String> list=Util.getMatchList(tempContent, "	<a", "0.jpg");
		tempContent=getDocumentAt(url+"rentisheying/");
		List<String> list2=Util.getMatchList(tempContent, "	<a", "0.jpg");
		list.addAll(list2);
		tempContent=getDocumentAt(url+"renticaihui/");
		List<String> list3=Util.getMatchList(tempContent, "	<a", "0.jpg");
		list.addAll(list3);
//		System.out.println(list.size());
		tempContent=null;
		getListInfo(list);
	}
	
	public static void getListInfo(List<String> list){
String s=null;
String id;
		for(int i=0;i<list.size();i++){
s=list.get(i);
MsNode mn=new MsNode();
id=s.replaceAll("	<a href=\"/.*?/|.html.*?jpg","");
try{
int parseInt = Integer.parseInt(id);
if(parseInt<=number)continue;
mn.setTitle(s.replaceAll(".*?title=\"|\".*jpg",""));
mn.setHref(s.replaceAll(".*?<img src=\"|\".*jpg","").replace("0.jpg",""));
m.put(parseInt, mn);
}catch(Exception e){
	System.out.println(id);
	System.out.println(e);
}
//t.replaceAll(".*?alt=\"|\".*>","")
		
		}
		List<Integer> order = new ArrayList<Integer>();
		order.addAll(m.keySet());
		if (order.size() > 0) {
			Collections.sort(order);
			System.out.println("最大值是 " + order.get(order.size()-1) + "  总共：" + order.size());
		} else {
			System.out.println("no news!");
		}
		int num;
		MsNode mn;
		String title;
		String href;
		for (int i = 0; i < order.size(); i++) {
			num = order.get(i);
			mn = m.get(num);
			title=mn.getTitle();
			href=mn.getHref();
			System.out.print("开始："+num+title);
			download(num , href,title);
			System.out.println("     ★完成 还有" + (order.size() - 1 - i));
		}
	}
	public static String getDocumentAt(String urlString) {
		StringBuffer document = new StringBuffer();
		try {
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "GB2312"));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line + "\n");
			reader.close();

		} catch (MalformedURLException e) {
			System.out.println("Unable to connect to URL: " + urlString);
		} catch (IOException e) {
			System.out.println("IOException when connecting to URL: "
					+ urlString);
		}
		return document.toString();
	}
}
